import rxAjax from '@/assets/ajax.js';
import BpmInstApi from "../../../../api/rxBpm/bpmInst.js";
export default {
    computed:{
        parentVm(){
            var vm=this;
            while (vm && !vm.rootVm){
                vm=vm.$parent;
            }
            return vm;
        },
        rxForms(){
            return this.parentVm.$refs.rxForms;
        }
    },
    methods:{
        i18nFormBase(name,text,key){
            return this.$ti18(name,text,"detailButtonLang.formBase",key);
        },
        /**
         * 执行按钮。
         * @param btn
         */
        handMethod(btn) {
            //表单方法
            if(btn.type=="formMethod"){
                //执行表单方法。
                this.handFormMethod(btn);
                return;
            }
            //自定义方法
            if(btn.type=="custom"){
                this.handCustom(btn);
                return;
            }
            this.buttonLoading = {
                key:btn.method,
                loading:true,
            }
            //调用预定义方法。
            console.log('method',btn.method)
            this[btn.method](btn);
        },
        //确定的时候回调
        dialogConfirm(popupConfig){
            let self = this ;
            if(['supspendInst','restoreInst'].includes(popupConfig.method)){
                let status = {
                    //暂停
                    'supspendInst':'SUPSPEND',
                    'restoreInst':'RUNNING',
                }
                BpmInstApi.updateProcessStatus(this.parentVm.instId,status[popupConfig.method]).then(res=>{
                    if(res.success){
                        self.msgConfig.msgType = 'success';
                        self.msgConfig.messageText = res.message ;
                        self.$refs.message.open();
                        let t = setTimeout(()=>{
                            window.location.reload();
                            clearTimeout(t)
                        },600)
                    }
                });
            }else
            if(popupConfig.method === 'doDiscard'){
                BpmInstApi.cancelProcess(this.parentVm.instId,"作废").then(resp=> {
                    self.msgConfig.msgType = 'success';
                    self.msgConfig.messageText = res.message ;
                    self.$refs.message.open();
                    let t = setTimeout(()=>{
                        window.location.reload();
                        clearTimeout(t)
                    },600)
                });
            }else
            if(popupConfig.method === 'delInst'){
                BpmInstApi.delBpmInst({ ids: this.parentVm.instId }).then(res => {
                    if(res.success){
                        self.msgConfig.msgType = 'success';
                        self.msgConfig.messageText = res.message ;
                        self.$refs.message.open();
                        let t = setTimeout(()=>{
                            self.$router.go(-1);
                            window.location.reload();
                            clearTimeout(t)
                        },600)
                    }
                })
            }
        },
        //删除
        delInst(btn){
            this.popupConfig.method = btn.method ;
            this.popupConfig.content = this.i18nFormBase('delInfo','您确定需要删除选中的记录吗?') ;
            this.$refs.alertDialog && this.$refs.alertDialog.open()
        },
        //作废
        doDiscard(btn){
            this.popupConfig.method = btn.method ;
            this.popupConfig.content =this.i18nFormBase('delCancel','您确定需要作废当前流程吗?') ;
            this.$refs.alertDialog && this.$refs.alertDialog.open()
        },
        //恢复
        restoreInst(btn){
            this.popupConfig.method = btn.method ;
            this.popupConfig.content =this.i18nFormBase('restore','您确定需要恢复当前流程吗?');
            this.$refs.alertDialog && this.$refs.alertDialog.open()
        },
        //暂停
        supspendInst(btn){
            this.popupConfig.method = btn.method ;
            this.$refs.alertDialog && this.$refs.alertDialog.open();
        },
        /**
         * 处理按钮是否显示
         * @param btn
         */
        changeBtnHandle(bpmActionType,processConfig,buttons,formData){
            let newBtns = [];
            if(!bpmActionType){
                return buttons;
            }
            if(!buttons || buttons.length==0){
                return newBtns;
            }
            for(let btn of buttons){
                let isShow = this.canShowBtn(bpmActionType,processConfig,btn,formData);
                if(isShow){
                    newBtns.push(btn);
                }
            }
            return newBtns;
        },
        canShowBtn(bpmActionType,processConfig,btn,formData){
            if(!btn.config || !btn.config.showScript){
                return true;
            }

            //当前登陆人
            //上下文
            let context ={
                instId:""
            };
            let user = uni.getStorageSync("user")
            context = { ...context,...user };
            if(bpmActionType === "bpmTaskStart"){
                //流程实例ID
                context.instId =processConfig.bpmInst.instId;
                //流程状态
                context.instStatus =processConfig.bpmInst.status;
                //流程节点数据
                context.bpmNodeId =processConfig.taskConfig.key;
                context.bpmNodeCode =processConfig.taskConfig.alias;
                context.bpmNodeName =processConfig.taskConfig.name;
            }
            //表单数据
            let formJson=this.rxForms.getData();
            if(formData && Array.isArray(formData) && formData.length){
                let obj = {}
                for (let item of formData){
                    if(item && item.alias){
                        obj[item.alias] = item.data ;
                    }
                }
                formJson = obj;
            }
            try {
                let func=new Function("data","context",btn.config.showScript);
                let isShow =func.bind(this)(formJson,context);
                return isShow;
            }catch (e){
                console.log("是否显示按钮计算出错。默认不显示，错误信息：  "+e);
            }

            return false;
        },
        /**
         * 处理自定义方法。
         * @param btn
         */
        handCustom(btn){
            let rxAjax = this.rxAjax;
            let formJson=this.rxForms.getData();
            let config=btn.config;
            let func=new Function("rxAjax","formJson",config.action);
            func.bind(this)(rxAjax,formJson);
        },
        getData(){
            var defId=this.parentVm.localDefId;
            var data={defId:defId,isMobile: 'YES'};
            var formType=this.parentVm.processConfig.formType;
            var hasPk=this.parentVm.processConfig.hasPk;
            var formData={};
            if(formType=="online"){
                if(this.rxForms){
                    formData=this.rxForms.getData();
                }
                data.systemHand=true;
                data.hasPk=hasPk;
            }

            var formJson=JSON.stringify(formData);
            data.formJson=formJson;

            let isCopy=this.parentVm.isCopy;
            if(this.parentVm.instId && !isCopy){
                data.instId=this.parentVm.instId;
            }
            data.attachment=JSON.stringify( this.parentVm.attachments);
            data.relateProcess=JSON.stringify(this.parentVm.relateProcess);
            return data;
        },
    }
}
